Welcome to the second assignment of Course 2!
In this assignment, you'll gain experience with tree based models by predicting the 10-year risk of death of individuals from the NHANES I epidemiology dataset (for a detailed description of this dataset you can check the CDC Website). This is a challenging task and a great test bed for the machine learning methods we learned this week.
As you go through the assignment, you'll learn about:
We'll first import all the common packages that we need for this assignment.
shap
is a library that explains predictions made by machine learning models.sklearn
is one of the most popular machine learning libraries.itertools
allows us to conveniently manipulate iterable objects such as lists.pydotplus
is used together with IPython.display.Image
to visualize graph structures such as decision trees.numpy
is a fundamental package for scientific computing in Python.pandas
is what we'll use to manipulate our data.seaborn
is a plotting library which has some convenient functions for visualizing missing data.matplotlib
is a plotting library.import shap
import sklearn
import itertools
import pydotplus
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from IPython.display import Image
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer, SimpleImputer
# We'll also import some helper functions that will be useful later on.
from util import load_data, cindex
Run the next cell to load in the NHANES I epidemiology dataset. This dataset contains various features of hospital patients as well as their outcomes, i.e. whether or not they died within 10 years.
X_dev, X_test, y_dev, y_test = load_data(10)
The dataset has been split into a development set (or dev set), which we will use to develop our risk models, and a test set, which we will use to test our models.
We further split the dev set into a training and validation set, respectively to train and tune our models, using a 75/25 split (note that we set a random state to make this split repeatable).
X_train, X_val, y_train, y_val = train_test_split(X_dev, y_dev, test_size=0.25, random_state=10)
The first step is to familiarize yourself with the data. Run the next cell to get the size of your training set and look at a small sample.
print("X_train shape: {}".format(X_train.shape))
X_train.head()
Our targets y
will be whether or not the target died within 10 years. Run the next cell to see the target data series.
y_train.head(20)
Use the next cell to examine individual cases and familiarize yourself with the features.
i = 10
print(X_train.iloc[i,:])
print("\nDied within 10 years? {}".format(y_train.loc[y_train.index[i]]))
Looking at our data in X_train
, we see that some of the data is missing: some values in the output of the previous cell are marked as NaN
("not a number").
Missing data is a common occurrence in data analysis, that can be due to a variety of reasons, such as measuring instrument malfunction, respondents not willing or not able to supply information, and errors in the data collection process.
Let's examine the missing data pattern. seaborn
is an alternative to matplotlib
that has some convenient plotting functions for data analysis. We can use its heatmap
function to easily visualize the missing data pattern.
Run the cell below to plot the missing data:
sns.heatmap(X_train.isnull(), cbar=False)
plt.title("Training")
plt.show()
sns.heatmap(X_val.isnull(), cbar=False)
plt.title("Validation")
plt.show()
For each feature, represented as a column, values that are present are shown in black, and missing values are set in a light color.
From this plot, we can see that many values are missing for systolic blood pressure (Systolic BP
).
In the cell below, write a function to compute the fraction of cases with missing data. This will help us decide how we handle this missing data in the future.
pandas.DataFrame.isnull()
method is helpful in this case.pandas.DataFrame.any()
method and set the axis
parameter.True
values are equal to 1.# UNQ_C1 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
def fraction_rows_missing(df):
'''
Return percent of rows with any missing
data in the dataframe.
Input:
df (dataframe): a pandas dataframe with potentially missing data
Output:
frac_missing (float): fraction of rows with missing data
'''
### START CODE HERE (REPLACE 'Pass' with your 'return' code) ###
missing = df.isnull().any(axis=1)
return missing.sum() / df.shape[0]
### END CODE HERE ###
Test your function by running the cell below.
df_test = pd.DataFrame({'a':[None, 1, 1, None], 'b':[1, None, 0, 1]})
print("Example dataframe:\n")
print(df_test)
print("\nComputed fraction missing: {}, expected: {}".format(fraction_rows_missing(df_test), 0.75))
print(f"Fraction of rows missing from X_train: {fraction_rows_missing(X_train):.3f}")
print(f"Fraction of rows missing from X_val: {fraction_rows_missing(X_val):.3f}")
print(f"Fraction of rows missing from X_test: {fraction_rows_missing(X_test):.3f}")
We see that our train and validation sets have missing values, but luckily our test set has complete cases.
As a first pass, we will begin with a complete case analysis, dropping all of the rows with any missing data. Run the following cell to drop these rows from our train and validation sets.
X_train_dropped = X_train.dropna(axis='rows')
y_train_dropped = y_train.loc[X_train_dropped.index]
X_val_dropped = X_val.dropna(axis='rows')
y_val_dropped = y_val.loc[X_val_dropped.index]
Having just learned about decision trees, you choose to use a decision tree classifier. Use scikit-learn to build a decision tree for the hospital dataset using the train set.
dt = DecisionTreeClassifier(max_depth=None, random_state=10)
dt.fit(X_train_dropped, y_train_dropped)
Next we will evaluate our model. We'll use C-Index for evaluation.
Remember from lesson 4 of week 1 that the C-Index evaluates the ability of a model to differentiate between different classes, by quantifying how often, when considering all pairs of patients (A, B), the model says that patient A has a higher risk score than patient B when, in the observed data, patient A actually died and patient B actually lived. In our case, our model is a binary classifier, where each risk score is either 1 (the model predicts that the patient will die) or 0 (the patient will live).
More formally, defining permissible pairs of patients as pairs where the outcomes are different, concordant pairs as permissible pairs where the patient that died had a higher risk score (i.e. our model predicted 1 for the patient that died and 0 for the one that lived), and ties as permissible pairs where the risk scores were equal (i.e. our model predicted 1 for both patients or 0 for both patients), the C-Index is equal to:
$$\text{C-Index} = \frac{\#\text{concordant pairs} + 0.5\times \#\text{ties}}{\#\text{permissible pairs}}$$
Run the next cell to compute the C-Index on the train and validation set (we've given you an implementation this time).
y_train_preds = dt.predict_proba(X_train_dropped)[:, 1]
print(f"Train C-Index: {cindex(y_train_dropped.values, y_train_preds)}")
y_val_preds = dt.predict_proba(X_val_dropped)[:, 1]
print(f"Val C-Index: {cindex(y_val_dropped.values, y_val_preds)}")
Unfortunately your tree seems to be overfitting: it fits the training data so closely that it doesn't generalize well to other samples such as those from the validation set.
The training C-index comes out to 1.0 because, when initializing
DecisionTreeClasifier
, we have leftmax_depth
andmin_samples_split
unspecified. The resulting decision tree will therefore keep splitting as far as it can, which pretty much guarantees a pure fit to the training data.
To handle this, you can change some of the hyperparameters of our tree.
Try and find a set of hyperparameters that improves the generalization to the validation set and recompute the C-index. If you do it right, you should get C-index above 0.6 for the validation set.
You can refer to the documentation for the sklearn DecisionTreeClassifier.
'max_depth'
).# Experiment with different hyperparameters for the DecisionTreeClassifier
# until you get a c-index above 0.6 for the validation set
dt_hyperparams = {
# set your own hyperparameters below, such as 'min_samples_split': 1
### START CODE HERE ###
'min_samples_split':60,
'max_depth' : 10
### END CODE HERE ###
}
Run the next cell to fit and evaluate the regularized tree.
# UNQ_C2 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
dt_reg = DecisionTreeClassifier(**dt_hyperparams, random_state=10)
dt_reg.fit(X_train_dropped, y_train_dropped)
y_train_preds = dt_reg.predict_proba(X_train_dropped)[:, 1]
y_val_preds = dt_reg.predict_proba(X_val_dropped)[:, 1]
print(f"Train C-Index: {cindex(y_train_dropped.values, y_train_preds)}")
print(f"Val C-Index (expected > 0.6): {cindex(y_val_dropped.values, y_val_preds)}")
If you used a low max_depth
you can print the entire tree. This allows for easy interpretability. Run the next cell to print the tree splits.
dot_data = StringIO()
export_graphviz(dt_reg, feature_names=X_train_dropped.columns, out_file=dot_data,
filled=True, rounded=True, proportion=True, special_characters=True,
impurity=False, class_names=['neg', 'pos'], precision=2)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
Overfitting, underfitting, and the bias-variance tradeoff
If you tested several values of
max_depth
, you may have seen that a value of3
gives training and validation C-Indices of about0.689
and0.630
, and that amax_depth
of2
gives better agreement with values of about0.653
and0.607
. In the latter case, we have further reduced overfitting, at the cost of a minor loss in predictive performance.Contrast this with a
max_depth
value of1
, which results in C-Indices of about0.597
for the training set and0.598
for the validation set: we have eliminated overfitting but with a much stronger degradation of predictive performance.Lower predictive performance on the training and validation sets is indicative of the model underfitting the data: it neither learns enough from the training data nor is able to generalize to unseen data (the validation data in our case).
Finding a model that minimizes and acceptably balances underfitting and overfitting (e.g. selecting the model with a
max_depth
of2
over the other values) is a common problem in machine learning that is known as the bias-variance tradeoff.
No matter how you choose hyperparameters, a single decision tree is prone to overfitting. To solve this problem, you can try random forests, which combine predictions from many different trees to create a robust classifier.
As before, we will use scikit-learn to build a random forest for the data. We will use the default hyperparameters.
rf = RandomForestClassifier(n_estimators=100, random_state=10)
rf.fit(X_train_dropped, y_train_dropped)
Now compute and report the C-Index for the random forest on the training and validation set.
y_train_rf_preds = rf.predict_proba(X_train_dropped)[:, 1]
print(f"Train C-Index: {cindex(y_train_dropped.values, y_train_rf_preds)}")
y_val_rf_preds = rf.predict_proba(X_val_dropped)[:, 1]
print(f"Val C-Index: {cindex(y_val_dropped.values, y_val_rf_preds)}")
Training a random forest with the default hyperparameters results in a model that has better predictive performance than individual decision trees as in the previous section, but this model is overfitting.
We therefore need to tune (or optimize) the hyperparameters, to find a model that both has good predictive performance and minimizes overfitting.
The hyperparameters we choose to adjust will be:
n_estimators
: the number of trees used in the forest.max_depth
: the maximum depth of each tree.min_samples_leaf
: the minimum number (if int
) or proportion (if float
) of samples in a leaf.The approach we implement to tune the hyperparameters is known as a grid search:
We define a set of possible values for each of the target hyperparameters.
A model is trained and evaluated for every possible combination of hyperparameters.
The best performing set of hyperparameters is returned.
The cell below implements a hyperparameter grid search, using the C-Index to evaluate each tested model.
def holdout_grid_search(clf, X_train_hp, y_train_hp, X_val_hp, y_val_hp, hyperparams, fixed_hyperparams={}):
'''
Conduct hyperparameter grid search on hold out validation set. Use holdout validation.
Hyperparameters are input as a dictionary mapping each hyperparameter name to the
range of values they should iterate over. Use the cindex function as your evaluation
function.
Input:
clf: sklearn classifier
X_train_hp (dataframe): dataframe for training set input variables
y_train_hp (dataframe): dataframe for training set targets
X_val_hp (dataframe): dataframe for validation set input variables
y_val_hp (dataframe): dataframe for validation set targets
hyperparams (dict): hyperparameter dictionary mapping hyperparameter
names to range of values for grid search
fixed_hyperparams (dict): dictionary of fixed hyperparameters that
are not included in the grid search
Output:
best_estimator (sklearn classifier): fitted sklearn classifier with best performance on
validation set
best_hyperparams (dict): hyperparameter dictionary mapping hyperparameter
names to values in best_estimator
'''
best_estimator = None
best_hyperparams = {}
# hold best running score
best_score = 0.0
# get list of param values
lists = hyperparams.values()
# get all param combinations
param_combinations = list(itertools.product(*lists))
total_param_combinations = len(param_combinations)
# iterate through param combinations
for i, params in enumerate(param_combinations, 1):
# fill param dict with params
param_dict = {}
for param_index, param_name in enumerate(hyperparams):
param_dict[param_name] = params[param_index]
# create estimator with specified params
estimator = clf(**param_dict, **fixed_hyperparams)
# fit estimator
estimator.fit(X_train_hp, y_train_hp)
# get predictions on validation set
preds = estimator.predict_proba(X_val_hp)
# compute cindex for predictions
estimator_score = cindex(y_val_hp, preds[:,1])
print(f'[{i}/{total_param_combinations}] {param_dict}')
print(f'Val C-Index: {estimator_score}\n')
# if new high score, update high score, best estimator
# and best params
if estimator_score >= best_score:
best_score = estimator_score
best_estimator = estimator
best_hyperparams = param_dict
# add fixed hyperparamters to best combination of variable hyperparameters
best_hyperparams.update(fixed_hyperparams)
return best_estimator, best_hyperparams
In the cell below, define the values you want to run the hyperparameter grid search on, and run the cell to find the best-performing set of hyperparameters.
Your objective is to get a C-Index above 0.6
on both the train and validation set.
def random_forest_grid_search(X_train_dropped, y_train_dropped, X_val_dropped, y_val_dropped):
# Define ranges for the chosen random forest hyperparameters
hyperparams = {
### START CODE HERE (REPLACE array values with your code) ###
# how many trees should be in the forest (int)
'n_estimators': range(110, 130, 5),
# the maximum depth of trees in the forest (int)
'max_depth': range(2, 30, 5),
# the minimum number of samples in a leaf as a fraction
# of the total number of samples in the training set
# Can be int (in which case that is the minimum number)
# or float (in which case the minimum is that fraction of the
# number of training set samples)
'min_samples_leaf': range(15, 50, 5)
### END CODE HERE ###
}
fixed_hyperparams = {
'random_state': 10,
}
rf = RandomForestClassifier
best_rf, best_hyperparams = holdout_grid_search(rf, X_train_dropped, y_train_dropped,
X_val_dropped, y_val_dropped, hyperparams,
fixed_hyperparams)
print(f"Best hyperparameters:\n{best_hyperparams}")
y_train_best = best_rf.predict_proba(X_train_dropped)[:, 1]
print(f"Train C-Index: {cindex(y_train_dropped, y_train_best)}")
y_val_best = best_rf.predict_proba(X_val_dropped)[:, 1]
print(f"Val C-Index: {cindex(y_val_dropped, y_val_best)}")
# add fixed hyperparamters to best combination of variable hyperparameters
best_hyperparams.update(fixed_hyperparams)
return best_rf, best_hyperparams
best_rf, best_hyperparams = random_forest_grid_search(X_train_dropped, y_train_dropped, X_val_dropped, y_val_dropped)
Finally, evaluate the model on the test set. This is a crucial step, as trying out many combinations of hyperparameters and evaluating them on the validation set could result in a model that ends up overfitting the validation set. We therefore need to check if the model performs well on unseen data, which is the role of the test set, which we have held out until now.
# UNQ_C3 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
y_test_best = best_rf.predict_proba(X_test)[:, 1]
print(f"Test C-Index: {cindex(y_test.values, y_test_best)}")
Your C-Index on the test set should be greater than 0.6
.
You've now built and optimized a random forest model on our data. However, there was still a drop in test C-Index. This might be because you threw away more than half of the data of our data because of missing values for systolic blood pressure. Instead, we can try filling in, or imputing, these values.
First, let's explore to see if our data is missing at random or not. Let's plot histograms of the dropped rows against each of the covariates (aside from systolic blood pressure) to see if there is a trend. Compare these to the histograms of the feature in the entire dataset. Try to see if one of the covariates has a signficantly different distribution in the two subsets.
dropped_rows = X_train[X_train.isnull().any(axis=1)]
columns_except_Systolic_BP = [col for col in X_train.columns if col not in ['Systolic BP']]
for col in columns_except_Systolic_BP:
sns.distplot(X_train.loc[:, col], norm_hist=True, kde=False, label='full data')
sns.distplot(dropped_rows.loc[:, col], norm_hist=True, kde=False, label='without missing data')
plt.legend()
plt.show()
sns.scatterplot(x=X_train_dropped['Age'], y= X_train_dropped['Systolic BP'], hue = y_train_dropped)
Most of the covariates are distributed similarly whether or not we have discarded rows with missing data. In other words missingness of the data is independent of these covariates.
If this had been true across all covariates, then the data would have been said to be missing completely at random (MCAR).
But when considering the age covariate, we see that much more data tends to be missing for patients over 65. The reason could be that blood pressure was measured less frequently for old people to avoid placing additional burden on them.
As missingness is related to one or more covariates, the missing data is said to be missing at random (MAR).
Based on the information we have, there is however no reason to believe that the values of the missing data — or specifically the values of the missing systolic blood pressures — are related to the age of the patients. If this was the case, then this data would be said to be missing not at random (MNAR).
mask = X_test['BMI'] < 20
. # UNQ_C4 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
def bad_subset(forest, X_test, y_test):
# define mask to select large subset with poor performance
# currently mask defines the entire set
### START CODE HERE (REPLACE the code after 'mask =' with your code) ###
mask = X_test['Age'] < 40
### END CODE HERE ###
X_subgroup = X_test[mask]
y_subgroup = y_test[mask]
subgroup_size = len(X_subgroup)
y_subgroup_preds = forest.predict_proba(X_subgroup)[:, 1]
performance = cindex(y_subgroup.values, y_subgroup_preds)
return performance, subgroup_size
performance, subgroup_size = bad_subset(best_rf, X_test, y_test)
print("Subgroup size should greater than 250, performance should be less than 0.69 ")
print(f"Subgroup size: {subgroup_size}, C-Index: {performance}")
Note, your actual output will vary depending on the hyper-parameters that you chose and the mask that you chose.
Subgroup size: 586, C-Index: 0.6275
Bonus:
Subgroup size: 251, C-Index: 0.5331
Seeing that our data is not missing completely at random, we can handle the missing values by replacing them with substituted values based on the other values that we have. This is known as imputation.
The first imputation strategy that we will use is mean substitution: we will replace the missing values for each feature with the mean of the available values. In the next cell, use the SimpleImputer
from sklearn
to use mean imputation for the missing values.
# Impute values using the mean
imputer = SimpleImputer(strategy='mean')
imputer.fit(X_train)
X_train_mean_imputed = pd.DataFrame(imputer.transform(X_train), columns=X_train.columns)
X_val_mean_imputed = pd.DataFrame(imputer.transform(X_val), columns=X_val.columns)
# Define ranges for the random forest hyperparameter search
hyperparams = {
### START CODE HERE (REPLACE array values with your code) ###
# how many trees should be in the forest (int)
'n_estimators': range(110, 130, 5),
# the maximum depth of trees in the forest (int)
'max_depth': range(3, 20, 3),
# the minimum number of samples in a leaf as a fraction
# of the total number of samples in the training set
# Can be int (in which case that is the minimum number)
# or float (in which case the minimum is that fraction of the
# number of training set samples)
'min_samples_leaf': range(5,40,5),
### END CODE HERE ###
}
# UNQ_C5 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
rf = RandomForestClassifier
rf_mean_imputed, best_hyperparams_mean_imputed = holdout_grid_search(rf, X_train_mean_imputed, y_train,
X_val_mean_imputed, y_val,
hyperparams, {'random_state': 10})
print("Performance for best hyperparameters:")
y_train_best = rf_mean_imputed.predict_proba(X_train_mean_imputed)[:, 1]
print(f"- Train C-Index: {cindex(y_train, y_train_best):.4f}")
y_val_best = rf_mean_imputed.predict_proba(X_val_mean_imputed)[:, 1]
print(f"- Val C-Index: {cindex(y_val, y_val_best):.4f}")
y_test_imp = rf_mean_imputed.predict_proba(X_test)[:, 1]
print(f"- Test C-Index: {cindex(y_test, y_test_imp):.4f}")
Note, your actual c-index values will vary depending on the hyper-parameters that you choose.
Performance for best hyperparameters:
- Train C-Index: 0.8109
- Val C-Index: 0.7495
- Test C-Index: 0.7805
Next, we will apply another imputation strategy, known as multivariate feature imputation, using scikit-learn's IterativeImputer
class (see the documentation).
With this strategy, for each feature that is missing values, a regression model is trained to predict observed values based on all of the other features, and the missing values are inferred using this model.
As a single iteration across all features may not be enough to impute all missing values, several iterations may be performed, hence the name of the class IterativeImputer
.
In the next cell, use IterativeImputer
to perform multivariate feature imputation.
Note that the first time the cell is run,
imputer.fit(X_train)
may fail with the messageLinAlgError: SVD did not converge
: simply re-run the cell.
# Impute using regression on other covariates
imputer = IterativeImputer(random_state=0, sample_posterior=False, max_iter=1, min_value=0)
imputer.fit(X_train)
X_train_imputed = pd.DataFrame(imputer.transform(X_train), columns=X_train.columns)
X_val_imputed = pd.DataFrame(imputer.transform(X_val), columns=X_val.columns)
# Define ranges for the random forest hyperparameter search
hyperparams = {
### START CODE HERE (REPLACE array values with your code) ###
# how many trees should be in the forest (int)
'n_estimators': range(110, 130, 5),
# the maximum depth of trees in the forest (int)
'max_depth': range(3, 20, 3),
# the minimum number of samples in a leaf as a fraction
# of the total number of samples in the training set
# Can be int (in which case that is the minimum number)
# or float (in which case the minimum is that fraction of the
# number of training set samples)
'min_samples_leaf': range(5,40,5),
### END CODE HERE ###
}
# UNQ_C6 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
rf = RandomForestClassifier
rf_imputed, best_hyperparams_imputed = holdout_grid_search(rf, X_train_imputed, y_train,
X_val_imputed, y_val,
hyperparams, {'random_state': 10})
print("Performance for best hyperparameters:")
y_train_best = rf_imputed.predict_proba(X_train_imputed)[:, 1]
print(f"- Train C-Index: {cindex(y_train, y_train_best):.4f}")
y_val_best = rf_imputed.predict_proba(X_val_imputed)[:, 1]
print(f"- Val C-Index: {cindex(y_val, y_val_best):.4f}")
y_test_imp = rf_imputed.predict_proba(X_test)[:, 1]
print(f"- Test C-Index: {cindex(y_test, y_test_imp):.4f}")
Note, your actual output will vary depending on the hyper-parameters that you chose and the mask that you chose.
Performance for best hyperparameters:
- Train C-Index: 0.8131
- Val C-Index: 0.7454
- Test C-Index: 0.7797
For good measure, retest on the subgroup from before to see if your new models do better.
performance, subgroup_size = bad_subset(best_rf, X_test, y_test)
print(f"C-Index (no imputation): {performance}")
performance, subgroup_size = bad_subset(rf_mean_imputed, X_test, y_test)
print(f"C-Index (mean imputation): {performance}")
performance, subgroup_size = bad_subset(rf_imputed, X_test, y_test)
print(f"C-Index (multivariate feature imputation): {performance}")
We should see that avoiding complete case analysis (i.e. analysis only on observations for which there is no missing data) allows our model to generalize a bit better. Remember to examine your missing cases to judge whether they are missing at random or not!
Using a random forest has improved results, but we've lost some of the natural interpretability of trees. In this section we'll try to explain the predictions using slightly more sophisticated techniques.
You choose to apply SHAP (SHapley Additive exPlanations) , a cutting edge method that explains predictions made by black-box machine learning models (i.e. models which are too complex to be understandable by humans as is).
Given a prediction made by a machine learning model, SHAP values explain the prediction by quantifying the additive importance of each feature to the prediction. SHAP values have their roots in cooperative game theory, where Shapley values are used to quantify the contribution of each player to the game.
Although it is computationally expensive to compute SHAP values for general black-box models, in the case of trees and forests there exists a fast polynomial-time algorithm. For more details, see the TreeShap paper.
We'll use the shap library to do this for our random forest model. Run the next cell to output the most at risk individuals in the test set according to our model.
X_test_risk = X_test.copy(deep=True)
X_test_risk.loc[:, 'risk'] = rf_imputed.predict_proba(X_test_risk)[:, 1]
X_test_risk = X_test_risk.sort_values(by='risk', ascending=False)
X_test_risk.head()
We can use SHAP values to try and understand the model output on specific individuals using force plots. Run the cell below to see a force plot on the riskiest individual.
explainer = shap.TreeExplainer(rf_imputed)
i = 0
shap_value = explainer.shap_values(X_test.loc[X_test_risk.index[i], :])[1]
shap.force_plot(explainer.expected_value[1], shap_value, feature_names=X_test.columns, matplotlib=True)
How to read this chart:
We can also use SHAP values to understand the model output in aggregate. Run the next cell to initialize the SHAP values (this may take a few minutes).
shap_values = shap.TreeExplainer(rf_imputed).shap_values(X_test)[1]
Run the next cell to see a summary plot of the SHAP values for each feature on each of the test examples. The colors indicate the value of the feature.
shap.summary_plot(shap_values, X_test)
Clearly we see that being a woman (sex = 2.0
, as opposed to men for which sex = 1.0
) has a negative SHAP value, meaning that it reduces the risk of dying within 10 years. High age and high systolic blood pressure have positive SHAP values, and are therefore related to increased mortality.
You can see how features interact using dependence plots. These plot the SHAP value for a given feature for each data point, and color the points in using the value for another feature. This lets us begin to explain the variation in SHAP value for a single value of the main feature.
Run the next cell to see the interaction between Age and Sex.
shap.dependence_plot('Age', shap_values, X_test, interaction_index='Sex')
We see that while Age > 50 is generally bad (positive SHAP value), being a woman generally reduces the impact of age. This makes sense since we know that women generally live longer than men.
Let's now look at poverty index and age.
shap.dependence_plot('Poverty index', shap_values, X_test, interaction_index='Age')
We see that the impact of poverty index drops off quickly, and for higher income individuals age begins to explain much of variation in the impact of poverty index.
Try some other pairs and see what other interesting relationships you can find!
You have completed the second assignment in Course 2. Along the way you've learned to fit decision trees, random forests, and deal with missing data. Now you're ready to move on to week 3!